const { cancelOrder } = require('../../../api/order');

// pages/mine/myOrder/myOrder.js
const app = getApp(),
    globalData = app.globalData,
    api = require('../../../api/index'),
    utils = require('../../../utils/util');
Page({

    /**
     * 页面的初始数据
     */
    data: {
        showModalId: '',
        showModal: false,
        reason: '',
        active: 0,
        orderList: [],
        status: 0,
        filter: {
            name: "",
            status: null,
            orderNo: "",
            type: null,
            currenetPageIndex: 1,
            pageSize: 10,
            recordCount: 0
        },
        toast: {
            visible: false,
            text: ''
        },
        hasMore: false
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        this.switchTap(options.index)
    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {
        if (this.data.hasMore) {
            this.setData({
                'filter.currenetPageIndex': ++this.data.filter.currenetPageIndex
            });

            this.getOrderList();
        }
    },

    switchTap(e) {
        console.log(e);
        let tab = ""
        if (e.currentTarget == undefined || null || "") {
            tab = e
        } else {
            tab = e.currentTarget.dataset.tab
        }
        console.log(tab, "...");
        this.setData({
            active: tab,
            "filter.status": Number(tab),
            "filter.currenetPageIndex": 1
        })
        this.getOrderList();
        wx.pageScrollTo({
            scrollTop: 0,
        })
    },

    toOrderDetail(e) {
        let id = e.currentTarget.dataset.id;
        console.log(id);
        wx.navigateTo({
            url: `/pages/mine/myOrder/orderDetail/orderDetail?id=${id}`,
        })
    },

    reload() {
        this.setData({
            status: 0
        })
        this.getOrderList();
    },

    // 获取订单列表
    async getOrderList() {
        wx.showLoading({
            title: '内容加载中',
        })
        try {
            let res = await api.order.orderList(this.data.filter),
                data = res.data.ResData || {};
            console.log(res, '订单列表')
            if (this.data.filter.currenetPageIndex > 1) {
                data.Items = this.data.list.concat(data.Items);
            }
            this.setData({
                list: data.Items || [],
                hasMore: data.TotalPages == 0 ? false : data.CurrentPage == data.TotalPages ? false : true,
                status: 2
            })
        } catch (e) {
            console.log(e, '获取订单列表失败')
            this.setData({
                status: 1
            })
        } finally {
            wx.hideLoading()
        }
    },

    // 申请售后
    applyAfterSales(e) {
        let id = e.currentTarget.dataset.id;
        console.log(e)
        this.setData({
            showModal: true,
            showModalId: id
        })
    },
    bindTextAreaBlur(e) {
        this.setData({
            reason: e.detail.value
        })
    },
    apply() {
        let that = this
        if(that.data.reason == '') return wx.showToast({
          title: '请填写申请的问题',
          icon: 'none'
        })
        api.order.getApplyAfterSales({
            order_id: that.data.showModalId,
            reason: that.data.reason
        }).then(res => {
            console.log(res)
            if (res.data.ErrCode == 0) {
                wx.showToast({
                    title: '申请成功，等待审核',
                    icon: 'none'
                })
                that.setData({
                    showModal: false
                })
                that.getOrderList()
            } else {
                wx.showToast({
                    title: res.data.ErrMsg,
                    icon: 'none'
                })
                that.setData({
                    showModal: false
                })
            }

        })
    },
    // 取消弹框
    close(){
        let that = this
        that.setData({
            showModal: false,
            showModalId: '',
            reason: ''
        })
    },

    // 取消订单
    async cancelOrder(e) {
        let id = e.currentTarget.dataset.id;
        try {
            let res = await api.order.cancelOrder(id);
            console.log(res, '取消订单')
            this.setData({
                toast: {
                    visible: true,
                    text: '取消订单成功'
                }
            })
            this.getOrderList()
        } catch (e) {
            console.log(e, '取消订单失败')
            this.setData({
                toast: {
                    visible: true,
                    text: '订单取消失败'
                }
            })
        }
    },

    // 删除订单
    async delOrder(e) {
        let id = e.currentTarget.dataset.id;
        console.log(id);
        wx.showLoading({
            title: '删除中',
        })
        console.log(id);
        let arr = []
        arr.push(id)

        try {
            let res = await api.order.deleteOrder(arr);
            console.log(res, '删除订单成功')
            this.setData({
                toast: {
                    visible: true,
                    text: '删除成功'
                }
            })
            this.getOrderList()
        } catch (e) {
            console.log(e, '删除订单失败')
            this.setData({
                toast: {
                    visible: true,
                    text: '删除失败'
                }
            })
        } finally {
            wx.hideLoading()
        }
    }
})